package com.pv.bfc.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.pv.bfc.dao.UserRepository;
import com.pv.bfc.model.BriefcaseUser;
import com.pv.bfc.utils.SendPassword;
import com.pv.bfc.web.validator.ForgetPasswordValidator;

@SuppressWarnings("deprecation")
public class ForgotPasswordController extends SimpleFormController {
	UserRepository userRepo;
	SendPassword sendPass;

	private static final Log logger = LogFactory
			.getLog(ForgotPasswordController.class);

	public ForgotPasswordController() {
		setCommandClass(BriefcaseUser.class);
		setSuccessView("redirect:passwordSend.jsp");
		setValidator(new ForgetPasswordValidator());
	}

	@Override
	protected void doSubmitAction(Object info) throws Exception {
		logger.info("IN ForgotPasswordController - doSubmitAction");

		BriefcaseUser user = (BriefcaseUser) info;
		BriefcaseUser bfcUser = userRepo.retreiveUserByUserName(user
				.getUserName());
		if (bfcUser != null) {
			sendPass.sendEmail(bfcUser);
		}

		logger.info("OUT ForgotPasswordController - doSubmitAction");
	}

	public void setUserRepo(UserRepository userRepo) {
		this.userRepo = userRepo;
	}

	public void setSendPassword(SendPassword sendPwd) {
		this.sendPass = sendPwd;
	}
}
